package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.bean.MyNewBean;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.utils.TimeUtil;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/6/20.
 */
public class MyNewsAdapter extends BaseAdapter {
    Context context;
    List<MyNewBean> itemBeans;
    LayoutInflater inflater;

    public MyNewsAdapter(Context context, List<MyNewBean> itemBeans) {
        this.context = context;
        this.itemBeans = itemBeans;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holldler holldler = null;
        MyNewBean bean = (MyNewBean) getItem(i);
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_mynews, null);
            holldler = new Holldler(view);
            view.setTag(holldler);
        } else {
            holldler = (Holldler) view.getTag();
        }

        try {
            String s=bean.title;
            if (s.length()>18){
                holldler.name.setText(s.substring(0,15));
                holldler.name.append("...");
            }else{
                holldler.name.setText(s);
            }

            if (bean.object_type.equals("0")){//系统
                SetTypeface.set(holldler.image,IconSourse.system_msg);
                holldler.image.setBackgroundResource(R.drawable.blue_c8_item);

            }else{
                SetTypeface.set(holldler.image,IconSourse.project_msg);
                holldler.image.setBackgroundResource(R.drawable.yellow_c8_item);
            }

            holldler.time.setText(TimeUtil.getTime(bean.create_time + "000"));
            holldler.message.setText(bean.content);

        } catch (Exception e) {
        }

        return view;
    }


    class Holldler {

        RelativeLayout item;
        TextView   name, time, message,image;

        Holldler(View v) {
            item = (RelativeLayout) v.findViewById(R.id.mynews_item);
            image = (TextView) v.findViewById(R.id.mynews_image);
            message = (TextView) v.findViewById(R.id.mynews_message);
            name = (TextView) v.findViewById(R.id.mynews_name);
            time = (TextView) v.findViewById(R.id.mynews_time);
         }
    }
}
